﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using BusinessLogicLayer;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using System.Data;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


namespace ChamCong.Admin
{
    public partial class DSNV : BasePage
    {
        int _MaNV;
        static int ma;
        static string name;
        NhanVienBLL nvBLL = new NhanVienBLL();
        PhongBanBLL pbBLL = new PhongBanBLL();
        List<int> lstRole;
        QuanLyPhanQuyenBLL pqBLL = new QuanLyPhanQuyenBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            _MaNV = Convert.ToInt32(Session["MaNV"]);
            lstRole = pqBLL.LayMaRoleTuNV(_MaNV);
            if (lstRole.Contains(1) || lstRole.Contains(4))
            {
                Menu1.Visible = true;
                lbTrangThai.Visible = true;
                drTrangThai.Visible = true;
            }
            else
            {
                Menu1.Visible = false;
                lbTrangThai.Visible = false;
                drTrangThai.Visible = false;
            }
            //ma = 0;
            name = "";
            if (!Page.IsPostBack)
            {
                BindGrid(ma, name, 1);
                binDdlDept();
            }

        }

        public void BindGrid(int ma, string name, int trangthai)
        {
            //List<DGVNHANVIEN> dsnv = new List<DGVNHANVIEN>();
            
            if (lstRole.Contains(1) || lstRole.Contains(4) )
            {
                GridView1.DataSource = nvBLL.DSNhanVien(ma, name, trangthai);
            }
            else if (lstRole.Contains(2))
            {
                GridView1.DataSource = nvBLL.DSNhanVien((int)nvBLL.LayNhanVien(_MaNV).MaPB, name, 1);
            }
            else
            {
                GridView1.DataSource = nvBLL.DSNhanVien(Convert.ToInt32(nvBLL.LayNhanVien(_MaNV).MaPB), nvBLL.LayNhanVien(_MaNV).HoTen, 1);
            }
            // = dsnv;
            GridView1.DataBind();
        }
        public void binDdlDept()
        {

            if (lstRole.Contains(1) || lstRole.Contains(4))
            {
                DSPhongBan.DataSource = pbBLL.LayTatCa().ToList();
            }
            else
            {
                DSPhongBan.DataSource = pbBLL.LayTatCa().Where(p => p.MaPB == nvBLL.LayMaPBTuNV(_MaNV)).ToList();
            }

            DSPhongBan.DataTextField = "TenPB";
            DSPhongBan.DataValueField = "MaPB";
            DSPhongBan.DataBind();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            this.BindGrid(ma, name, 1);
        }

        protected void txtTen_TextChange(object sender, EventArgs e)
        {
            ma = int.Parse(DSPhongBan.SelectedValue);
            string name = ((TextBox)sender).Text;
            BindGrid(ma, name, 1);
        }

        protected void btnTatCa_Click(object sender, EventArgs e)
        {
            ma = 0;
            name = "";
            int tt = int.Parse(drTrangThai.SelectedValue);
            BindGrid(ma, name, tt);
        }

        protected DataTable ConvertToDataTable()
        {

            DataTable dt = new DataTable();

            // add the columns to the datatable            
            if (GridView1.HeaderRow != null)
            {
                int a = GridView1.HeaderRow.Cells.Count;
                for (int i = 0; i < (GridView1.HeaderRow.Cells.Count - 2); i++)
                {
                    dt.Columns.Add(GridView1.HeaderRow.Cells[i].Text);
                }
            }

            //  add each of the data rows to the table
            foreach (GridViewRow row in GridView1.Rows)
            {
                DataRow dr;
                dr = dt.NewRow();
                int b = row.Cells.Count;
                for (int i = 0; i < (row.Cells.Count - 2); i++)
                {
                    string x = row.Cells[i].Text;
                    dr[i] = row.Cells[i].Text;
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }



        protected string AutoConvert(object datetimenow)
        {
            //return (datetimenow != null ? string.Format("{0:dd/MM/yyyy}", Convert.ToDateTime(datetimenow)) : "No DateTime");
            if (datetimenow.ToString().Trim() == "12/31/9999 11:59:59 PM")
            {
                return "---";
            }
            else
            {
                return string.Format("{0:dd/MM/yyyy}", Convert.ToDateTime(datetimenow));
            }
        }

        protected void DSPhongBan_SelectedIndexChanged(object sender, EventArgs e)
        {
            //int ma = 0;
            ma = int.Parse(DSPhongBan.SelectedValue);
            name = txtTen.Text;
            BindGrid(ma, name, 1);
            //GridView1.DataBind();
        }

        protected void drTrangThai_SelectedIndexChanged(object sender, EventArgs e)
        {
            //ma = int.Parse(DSPhongBan.SelectedValue);
            name = txtTen.Text.Trim();
            int tt = int.Parse(drTrangThai.SelectedValue);
            BindGrid(0, name, tt);
        }

    }
}